package com.aqiang.day0528_shop.main.view.activity

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.*
import android.widget.PopupWindow
import android.widget.TextView
import com.aqiang.day0528_shop.R
import com.aqiang.day0528_shop.main.contract.GoodsTypeContract
import com.aqiang.day0528_shop.sql.UserInfo
import com.aqiang.lib_core.mvp.BaseActivity
import kotlinx.android.synthetic.main.activity_order.*
import kotlinx.android.synthetic.main.pop_windwo.*
import org.jetbrains.anko.startActivity

class OrderActivity : BaseActivity<GoodsTypeContract.GoodsTypePresenter>() {
    private var money:String = ""
    private var userInfo:UserInfo? = null
    override fun bindLatout(): Int {
        return R.layout.activity_order
    }

    override fun createPresenter() {

    }

    override fun initView(savedInstanceState: Bundle?) {

    }

    override fun initData() {
        money = intent.getStringExtra("money")
        if(money != null){
            tv_act_order_money.text = money
        }

    }

    override fun initEvent() {
        tv_act_order_add.setOnClickListener {
            startActivity<ReceiptActivity>()
        }
        ll_act_order_msg.setOnClickListener {
            startActivity<ReceiptActivity>()
        }
        tv_act_order_pay.setOnClickListener {
            var view = LayoutInflater.from(this).inflate(R.layout.pop_windwo,null)
            var zfb = view.findViewById<TextView>(R.id.tv_pop_online)
            zfb.setOnClickListener {
                tv_act_order_pay.text = zfb.text.toString()
            }
            var hd = view.findViewById<TextView>(R.id.tv_pop_hd)
            hd.setOnClickListener {
                tv_act_order_pay.text = hd.text.toString()
            }
            var zr = view.findViewById<TextView>(R.id.tv_pop_zr)
            zr.setOnClickListener {
                tv_act_order_pay.text = zr.text.toString()
            }
            val popupWindow = PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT,true)
            popupWindow.showAtLocation(it,Gravity.BOTTOM,0,0)
        }
    }

    override fun onResume() {
        super.onResume()
        userInfo = UserInfo.getInstance(this)
        userInfo!!.use {
            val cursor = query(UserInfo.sqlName, null, null, null, null, null, null)
            if(cursor != null){
                if(cursor.moveToNext()){
                    ll_act_order_msg.visibility = View.VISIBLE
                    tv_act_order_add.visibility = View.GONE
                    val name = cursor.getString(cursor.getColumnIndex("name"))
                    val tel = cursor.getString(cursor.getColumnIndex("tel"))
                    val area = cursor.getString(cursor.getColumnIndex("area"))
                    val address = cursor.getString(cursor.getColumnIndex("address"))
                    tv_act_order_name.text = name
                    tv_act_order_tel.text = tel
                    tv_act_order_province.text = area
                    tv_act_order_address.text = address
                }else{
                    tv_act_order_add.visibility = View.VISIBLE
                    ll_act_order_msg.visibility = View.GONE
                }
            }
        }
    }
}
